/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.graphic.objects;

import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Robot;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.graphic.locations.FlexibleRelativeLocation;
import model.graphic.locations.VerticalLineLocation;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Jelle
 */
public class GTransmitterNode extends GNode implements Serializable {

    private GBoxNode base;
    private boolean left;
    private String type;
    private String classDef;
    private int previousMouseX;
    private int previousMouseY;

    public GTransmitterNode(GBoxNode base, boolean left) {
        super(null, 10, 15);
        this.base = base;
        this.left = left;
        if (left) {
            setLocations(new VerticalLineLocation(new FlexibleRelativeLocation((base.location), -10, 0), new FlexibleRelativeLocation(base.location, -10, 0), new FlexibleRelativeLocation(base.lowerLeft, -10, -15)));
        } else {
            setLocations(new VerticalLineLocation(new FlexibleRelativeLocation(base.upperRight, 0, 0), new FlexibleRelativeLocation(base.upperRight, 0, 0), new FlexibleRelativeLocation(base.lowerRight, 0, -15)));
        }
    }

    @Override
    public void paint(Graphics2D g) {
        if (this.isVisible()) {
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();
            g.setColor(getBackColor());
            g.fillRect(location.getX(), location.getY(), getWidth(), getHeight());
            if (highlighted || selected) {
                g.setColor(highlightColor);
            } else {
                g.setColor(baseColor);
            }

            g.setStroke(new BasicStroke(2));
            g.drawRect(location.getX(), location.getY(), getWidth(), getHeight());
            g.setColor(oldColor);
            g.setStroke(oldStroke);
        }
    }

    /**
     * @return the base
     */
    public GBoxNode getBase() {
        return base;
    }

    /**
     * @return the left
     */
    public boolean isLeft() {
        return left;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the classDef
     */
    public String getClassDef() {
        return classDef;
    }

    /**
     * @param classDef the classDef to set
     */
    public void setClassDef(String classDef) {
        this.classDef = classDef;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        if (this.notclickedAddon) {
            int x = e.getX();
            int y = e.getY();
            int xn = this.getX() + x - dragEndPoint.x;
            int yn = this.getY() + y - dragEndPoint.y;
            this.getUpperLeft().setXY(xn, yn);
            this.setLocation(this.getUpperLeft());
            previousMouseX = (int) e.getLocationOnScreen().getX();
            previousMouseY = (int) e.getLocationOnScreen().getY();
        } else {
            Robot r;
            try {
                r = new Robot();
                r.mouseMove(previousMouseX, previousMouseY);
            } catch (AWTException ex) {
                Logger.getLogger(GNode.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        gv.repaint();
    }
}
